<?php
/**
 * Created by JetBrains PhpStorm.
 * User: FuXiaoHei
 * Date: 13-4-29
 * Time: 下午4:48
 * To change this template use File | Settings | File Templates.
 */

namespace API\App\Model;

use Hx\App;

class Comment {

    /**
     * 数据库对象
     * @var \Hx\Db
     */
    private $db;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->db = App::Db();
    }


    /**
     * 获取Gravatar头像
     * @param string $email
     * @return string
     */
    private function getAvatar($email) {
        return 'http://1.gravatar.com/avatar/' . md5($email) . '?s=40&r=G&d=http://ww1.sinaimg' .
            '.cn/large/67252e9ajw1dqn0haiinwj' .
            '.jpg';
    }

    public function getList($condition, $page, $size = 6) {
        $col = 'id,author,email,url,createDate,content,parentId,';
        $col .= 'status,userIp,userAgent';
        $sql = App::Sql('HxSite_Comment', $col)
            ->where('commentType = "comment" AND ' . $condition)
            ->order('id DESC')
            ->page($page, $size)
            ->select();
        $data['comment'] = $this->db->queryAll($sql);
        foreach ($data['comment'] as $k => $v) {
            $data['comment'][$k]->avatar = $this->getAvatar($v->email);
        }
        $sql = App::Sql('HxSite_Comment')
            ->where('commentType = "comment" AND ' . $condition)
            ->count();
        $all = $this->db->query($sql)->num;
        $data['count'] = array(
            'all' => $all,
            'pages' => (int)($all / $size + 0.999),
            'current' => $page,
            'begin' => ($page - 1) * $size >= $all ? $all : ($page - 1) * $size + 1,
            'end' => $page * $size >= $all ? $all : $page * $size
        );
        return $data;
    }

    public function getOne($id) {
        $col = 'id,author,email,url,createDate,content,parentId,';
        $col .= 'status,userIp,userAgent';
        $sql = App::Sql('HxSite_Comment', $col)
            ->where('id = :id')
            ->select();
        $data = $this->db->query($sql, array('id' => $id));
        if (!$data) {
            return false;
        }
        $data->avatar = $this->getAvatar($data->email);
        unset($data->email);
        return $data;
    }

    public function getTree($node) {
        $col = 'id,author,email,url,createDate,content,parentId,';
        $col .= 'status,userIp,userAgent';
        $sql = App::Sql('HxSite_Comment', $col)
            ->where('nodeId = :node AND commentType = "comment" AND status = "public"')
            ->order('id ASC')
            ->select();
        $commentData = $this->db->queryAll($sql, array('node' => $node));
        if ($commentData) {
            $tempData = array();
            foreach ($commentData as $c) {
                $c->avatar = $this->getAvatar($c->email);
                unset($c->email);
                $tempData[$c->id] = $c;
            }
            $commentData = array();
            foreach ($tempData as $k => $c) {
                if ($tempData[$c->parentId]) {
                    #将子评论移动到父评论麾下
                    $tempData[$c->parentId]->children[] =& $tempData[$k];
                    continue;
                }
                if ($c->parentId > 0) {
                    continue;
                }
                $commentData[] =& $tempData[$k];
            }
        }
        return $commentData;
    }

}